var tabObjectConfig = {
    tabName: "tsCms",                          //lay-filter名
    clickActive: ".ts-tab-active",             //按钮点击class绑定
    tabModel: "resources/View/",            //路径拼接
    tabUrlParameter: "mod",                    //URL参数名称
    contentKeyAttribute: "tbkey",        //获取标签属性，tab的内容标识
    tabTitleAttribute: "title",                 //获取标签属性，tab的标题
    popupUrl: "resources/Popup/",
}

layui.use('element', function () {
    var $ = layui.jquery
        , element = layui.element; //Tab的切换功能，切换事件监听等，需要依赖element模块;  
    layer.config({
        skin: 'ts-layer-theme'
    });
    $(function () {
        TabInitialization();
    });
    /**
     * 导航按钮点击
     */
    $("#nav-left").on("click", tabObjectConfig.clickActive, function () {
        var tabData = $(this);
        var title = tabData.attr(tabObjectConfig.tabTitleAttribute)
            , contentkey = tabData.attr(tabObjectConfig.contentKeyAttribute);
        //当不存在title属性
        if (title == null) title = tabData.text();
        tabAdd(title, contentkey);
        element.tabChange(tabObjectConfig.tabName, contentkey);
    })
    /*$(tabObjectConfig.clickActive).on('click', function () {
        var tabData = $(this);
        var title = tabData.attr(tabObjectConfig.tabTitleAttribute)
            , contentkey = tabData.attr(tabObjectConfig.contentKeyAttribute);
        //当不存在title属性
        if(title == null) title = tabData.text();   
        tabAdd(title, contentkey);
        element.tabChange(tabObjectConfig.tabName, contentkey);
    });*/
    /**
     * 刷新当前选项卡
     */
    $(".ts-tb-reload").click(function () {
        reloadTab();
    });

    $(".layui-header .layui-nav-item").click(function () {
        $(this).removeClass("layui-this");
    });
    /**
     * 延展事件
     */
    $("a[tsadmin-event='flexible']").click(function () {
        var flexible = $(this).attr("flexible");
        if (flexible == "true") {
            flexible = "false";
            $(".layui-layout.layui-layout-admin").removeClass("ts-flexible-none").addClass("ts-flexible");
            $(this).html('<i class="layui-icon layui-icon-shrink-right"></i>');
        } else {
            flexible = "true";
            $(".layui-layout.layui-layout-admin").removeClass("ts-flexible").addClass("ts-flexible-none");
            $(this).html('<i class="layui-icon layui-icon-spread-left"></i>');
        }
        $(this).attr("flexible", flexible);
    });
    $("a[tsadmin-event='fullscreen']").click(function () {
        var fullscreen = $(this).attr("fullscreen");
        if (fullscreen == "true") {
            breakFullScreen();
            fullscreen = "false";
            $(this).html('<i class="layui-icon layui-icon-screen-full"></i>');
        } else {
            setfullScreen();
            fullscreen = "true";
            $(this).html('<i class="layui-icon layui-icon-screen-restore"></i>');
        }

        $(this).attr("fullscreen", fullscreen);
    });
    $("a[tsadmin-event='changetheme']").click(function () {
        SetPopup("主题切换", "ChangeTheme", "400px");
    });
    function setfullScreen() {
        var docElm = document.documentElement;
        //W3C   
        if (docElm.requestFullscreen) {
            docElm.requestFullscreen();
        }
        //FireFox   
        else if (docElm.mozRequestFullScreen) {
            docElm.mozRequestFullScreen();
        }
        //Chrome等   
        else if (docElm.webkitRequestFullScreen) {
            docElm.webkitRequestFullScreen();
        }
        //IE11   
        else if (elem.msRequestFullscreen) {
            elem.msRequestFullscreen();
        }
    }
    function breakFullScreen() {
        if (document.exitFullscreen) {
            document.exitFullscreen();
        }
        else if (document.mozCancelFullScreen) {
            document.mozCancelFullScreen();
        }
        else if (document.webkitCancelFullScreen) {
            document.webkitCancelFullScreen();
        }
        else if (document.msExitFullscreen) {
            document.msExitFullscreen();
        }
    }
    function reloadTab() {
        var key = getQueryString(tabObjectConfig.tabUrlParameter)
            , checkTab = $(".layui-tab-title li[lay-id='" + key + "']");
        if (checkTab.length > 0) {
            changeTab(key);
            var tabNowName = checkTab.text().replace("ဆ", "");;
            $.ajax({
                type: "get"
                , url: tabObjectConfig.tabModel + key + ".html"
                , contentType: "application/x-www-form-urlencoded; charset=utf-8"
                , success: function (data) {
                    $("div.layui-tab-item.layui-show").html(data);
                    layer.msg(tabNowName + "页面刷新成功", { icon: 1 });
                }
            });
        }
    }
    element.on('tab(' + tabObjectConfig.tabName + ')', function () {
        var key = $(".layui-tab-title li.layui-this").attr("lay-id");
        updateQueryStringParameter(tabObjectConfig.tabUrlParameter, key);
    });
    /**
     * 切换tab
     * @param {*} tabid 
     */
    function changeTab(tabid) {
        element.tabChange(tabObjectConfig.tabName, tabid);
        //静态修改URL中mod参数
        updateQueryStringParameter(tabObjectConfig.tabUrlParameter, tabid);
    }
    /**
     * 
     * @param {*} title tab标题
     * @param {*} contentkey tab内容
     */
    function tabAdd(title, contentkey) {
        //获取选项卡
        var method = 'get';
        if (arguments.length >= 3) {
            if (arguments[2] == "post" || arguments[2] == "get") {
                method = arguments[2];
            }
        }
        var CheckTab = $(".layui-tab-title li[lay-id='" + contentkey + "']");
        //判断是否已存在该tab
        if (CheckTab.length > 0) {
            //切换tab
            changeTab(contentkey);
        } else {
            //异步获取tab页面
            $.ajax({
                type: method
                , url: tabObjectConfig.tabModel + contentkey + ".html"
                , contentType: "application/x-www-form-urlencoded; charset=utf-8"
                , success: function (data) {
                    //开始添加选项卡
                    element.tabAdd(tabObjectConfig.tabName, {
                        title: title
                        , content: data
                        , id: contentkey
                    });
                    //切换到刚添加完的选项卡
                    changeTab(contentkey);
                    //静态修改URL中mod参数
                    updateQueryStringParameter(tabObjectConfig.tabUrlParameter, contentkey);
                }
            });
        }
    }
    /**
     * 初始化tab
     */
    function TabInitialization() {
        //获取参数值
        var tabid = getQueryString(tabObjectConfig.tabUrlParameter);
        if (tabid != "") {
            var tabData = $("a" + tabObjectConfig.clickActive + "[" + tabObjectConfig.contentKeyAttribute + "='" + tabid + "']");
            //检查是否存在
            if (tabData.length > 0) {
                var title = tabData.attr(tabObjectConfig.tabTitleAttribute),
                    contentkey = tabData.attr(tabObjectConfig.contentKeyAttribute);
                if (title == null) title = tabData.text();
                if (tabAdd(title, contentkey)) {
                    //切换tab
                    changeTab(tabid);
                }
            }
        }
    }
});//element end



/**
 * 获取页面层弹窗
 * @param {*} title 弹窗标题
 * @param {*} fileName 异步获取弹窗内容主体路径
 * @param method 可选项，获取方式，默认为get方式,不做选择可为false
 * @param area 可选项，弹窗宽度，默认为600px
 */
function SetPopup(title, fileName, data = {}) {
    var method = 'get'  //获取方式，初始值为get
        , area = '600px'
        , offset = 'auto'
        , type  = 1
        , scrollbar = true;  //弹出层大小
    if (data.area != null) area = data.area;
    if (data.offset != null) offset = data.offset;
    if (data.type != null) type = data.type;
    if (data.scrollbar != null) scrollbar = data.scrollbar;

    //获取弹窗
    layui.use('layer', function () {
        var layer = layui.layer;
        layer.config({
            skin: 'ts-layer-theme'
        });
        $.ajax({
            type: method
            , url: tabObjectConfig.popupUrl + fileName + ".html"
            , success: function (result) {
                layer.open({
                    type: type
                    , title: title
                    , area: area
                    , offset: offset
                    ,scrollbar : scrollbar 
                    , content: result
                });
            }
        });
    });
}
/**
 * 
 * @param {*} title 
 * @param {*} contentkey 
 */
function tabAddByData(title, contentkey) {
    $.ajax({
        type: "post"
        , url: "resources/View/" + contentkey + ".html"
        , contentType: "application/x-www-form-urlencoded; charset=utf-8"
        , success: function (data) {
            element.tabAdd(tabName, {
                title: title
                , content: data
                , id: contentkey
            });
        }
    });
}

/**
 * 添加选项卡
 * 异步获取选项卡主体内容
 * @param {*} title 
 * @param {*} contentkey
 * @param method 可选项，获取方式，默认为get方式 
 */



/*
获取table数据
*/
function reloadTable(tbfilter, url) {
    table.reload(tbfilter, {
        url: url
        , where: {}
    });
}

/**
 * 设置Cookie
 * @param {*} cookieName 
 * @param {*} cookieValue 
 */
function setCookie(cookieName, cookieValue) {
    var data = { expires: 7 }, path = "";
    if (arguments.length == 3) {
        if (typeof arguments[2] == "string") {
            data = { expires: 7, path: arguments[2] };
        } else {
            data = { expires: arguments[2] };
        }
    } else if (arguments.length == 4) {
        data = { expires: arguments[2], path: arguments[3] };
    }
    $.cookie(cookieName, cookieValue, data);
}
/**
 * 获取Cookie
 * @param {*} cookieName 
 */
function getCookie(cookieName) {
    return $.cookie(cookieName);
}
/**
 * 删除Cookie
 * @param {*} cookieName 
 */
function deleteCookie(cookieName) {
    $.cookie(cookieName, null, { expires: -1 });
}

function changeStatus() {

}

function getDate(dateStr) {
    var date = new Date(dateStr)
        , mod = ["Y", "M", "D", "h", "m", "s"]
        , resDate = []
        , resultMod = "Y-M-D h:m:s";
    if (arguments.length == 2) {
        resultMod = arguments[1];
    }
    var Y = date.getFullYear(),
        M = Number(date.getMonth()) + 1
    D = date.getDay(),
        h = date.getHours(),
        m = date.getMinutes(),
        s = date.getSeconds();
    resDate.push(dateToNumber(Y));
    resDate.push(dateToNumber(M));
    resDate.push(dateToNumber(D));
    resDate.push(dateToNumber(h));
    resDate.push(dateToNumber(m));
    resDate.push(dateToNumber(s));
    for (var i in resDate) {
        resultMod = resultMod.replace(mod[i], resDate[i]);
    }
    return resultMod;
}
function dateToNumber(d) {
    d = d.toString();
    return d[1] ? d : '0' + d
}
function checkSwitch(value) {
    var res = '';
    if (value != 'not') {
        res = "checked";
    }
    return res;
}
function checkSex(sex) {
    var sext = "";
    if (sex == "男") {
        sext = " layui-bg-blue";
    }
    return sext;
}
function checkLock(value) {
    var res = '';
    if (value == 'not') {
        res = "checked";
    }
    return res;
}
function formRender(){
    layui.use('form', function () {
        var form = layui.form;
        form.render();
    });
}
const capitalize = ([first, ...rest], lowerRest = false) =>
    first.toUpperCase() + (lowerRest ? rest.join('').toLowerCase() : rest.join(''));